home *** CD-ROM | disk | FTP | other *** search
/ Team Palmtops 7 / Palmtops_numero07.iso / WinCE / SDKWindowsCE / HandHeldPCPro30 / sdk.exe / Jupiter SDK / data1.cab / MFC_Samples / maths2 / mathsctl.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-02-19  |  9.0 KB  |  331 lines

  1. // MathsCtl.cpp : Implementation of the CMaths2Ctrl OLE control class.
  2.  
  3. #include "stdafx.h"
  4. #include "Maths2.h"
  5. #include "MathsCtl.h"
  6. #include "MathsPpg.h"
  7.  
  8.  
  9. #ifdef _DEBUG
  10. #define new DEBUG_NEW
  11. #undef THIS_FILE
  12. static char THIS_FILE[] = __FILE__;
  13. #endif
  14.  
  15.  
  16. IMPLEMENT_DYNCREATE(CMaths2Ctrl, COleControl)
  17.  
  18.  
  19. /////////////////////////////////////////////////////////////////////////////
  20. // Message map
  21.  
  22. BEGIN_MESSAGE_MAP(CMaths2Ctrl, COleControl)
  23.     //{{AFX_MSG_MAP(CMaths2Ctrl)
  24.     ON_WM_LBUTTONDOWN()
  25.     //}}AFX_MSG_MAP
  26. END_MESSAGE_MAP()
  27.  
  28.  
  29. /////////////////////////////////////////////////////////////////////////////
  30. // Dispatch map
  31.  
  32. BEGIN_DISPATCH_MAP(CMaths2Ctrl, COleControl)
  33.     //{{AFX_DISPATCH_MAP(CMaths2Ctrl)
  34.     DISP_PROPERTY_NOTIFY(CMaths2Ctrl, "Opposite", m_bOpposite, OnOppositeChanged, VT_BOOL)
  35.     DISP_STOCKPROP_BACKCOLOR()
  36.     DISP_STOCKPROP_FORECOLOR()
  37.     //}}AFX_DISPATCH_MAP
  38.     DISP_FUNCTION_ID(CMaths2Ctrl, "AboutBox", DISPID_ABOUTBOX, AboutBox, VT_EMPTY, VTS_NONE)
  39. END_DISPATCH_MAP()
  40.  
  41.  
  42. /////////////////////////////////////////////////////////////////////////////
  43. // Event map
  44.  
  45. BEGIN_EVENT_MAP(CMaths2Ctrl, COleControl)
  46.     //{{AFX_EVENT_MAP(CMaths2Ctrl)
  47.     EVENT_CUSTOM("Outside", FireOutside, VTS_NONE)
  48.     EVENT_CUSTOM("Inside", FireInside, VTS_XPOS_PIXELS  VTS_YPOS_PIXELS)
  49.     //}}AFX_EVENT_MAP
  50. END_EVENT_MAP()
  51.  
  52.  
  53. /////////////////////////////////////////////////////////////////////////////
  54. // Property pages
  55.  
  56. BEGIN_PROPPAGEIDS(CMaths2Ctrl, 1)
  57.     PROPPAGEID(CMaths2PropPage::guid)
  58. END_PROPPAGEIDS(CMaths2Ctrl)
  59.  
  60.  
  61. /////////////////////////////////////////////////////////////////////////////
  62. // Initialize class factory and guid
  63.  
  64. IMPLEMENT_OLECREATE_EX(CMaths2Ctrl, "MATHS.Maths2Ctrl.1",
  65.     0x181CB003, 0xbeed, 0x11ce, 0x96, 0x11, 0, 0xaa, 0, 0x4a, 0x75, 0xcf)
  66.  
  67.  
  68. /////////////////////////////////////////////////////////////////////////////
  69. // Type library ID and version
  70.  
  71. IMPLEMENT_OLETYPELIB(CMaths2Ctrl, _tlid, _wVerMajor, _wVerMinor)
  72.  
  73.  
  74. /////////////////////////////////////////////////////////////////////////////
  75. // Interface IDs
  76.  
  77. const IID BASED_CODE IID_DMaths2 =
  78.         { 0x181cb001, 0xbeed, 0x11ce, { 0x96, 0x11, 0, 0xaa, 0, 0x4a, 0x75, 0xcf } };
  79. const IID BASED_CODE IID_DMaths2Events =
  80.         { 0x181cb002, 0xbeed, 0x11ce, { 0x96, 0x11, 0, 0xaa, 0, 0x4a, 0x75, 0xcf } };
  81.  
  82.  
  83. /////////////////////////////////////////////////////////////////////////////
  84. // Control type information
  85.  
  86. static const DWORD BASED_CODE _dwMaths2OleMisc =
  87.     OLEMISC_ACTIVATEWHENVISIBLE |
  88.     OLEMISC_SETCLIENTSITEFIRST |
  89.     OLEMISC_INSIDEOUT |
  90.     OLEMISC_CANTLINKINSIDE |
  91.     OLEMISC_RECOMPOSEONRESIZE;
  92.  
  93. IMPLEMENT_OLECTLTYPE(CMaths2Ctrl, IDS_Maths2, _dwMaths2OleMisc)
  94.  
  95.  
  96. /////////////////////////////////////////////////////////////////////////////
  97. // CMaths2Ctrl::CMaths2CtrlFactory::UpdateRegistry -
  98. // Adds or removes system registry entries for CMaths2Ctrl
  99.  
  100. BOOL CMaths2Ctrl::CMaths2CtrlFactory::UpdateRegistry(BOOL bRegister)
  101. {
  102.     if (bRegister)
  103.         return AfxOleRegisterControlClass(
  104.             AfxGetInstanceHandle(),
  105.             m_clsid,
  106.             m_lpszProgID,
  107.             IDS_Maths2,
  108.             IDB_Maths2,
  109.             FALSE,                      //  Not insertable
  110.             _dwMaths2OleMisc,
  111.             _tlid,
  112.             _wVerMajor,
  113.             _wVerMinor);
  114.     else
  115.         return AfxOleUnregisterClass(m_clsid, m_lpszProgID);
  116. }
  117.  
  118.  
  119. /////////////////////////////////////////////////////////////////////////////
  120. // CMaths2Ctrl::CMaths2Ctrl - Constructor
  121.  
  122. CMaths2Ctrl::CMaths2Ctrl()
  123. {
  124.     InitializeIIDs(&IID_DMaths2, &IID_DMaths2Events);
  125. }
  126.  
  127.  
  128. /////////////////////////////////////////////////////////////////////////////
  129. // CMaths2Ctrl::~CMaths2Ctrl - Destructor
  130.  
  131. CMaths2Ctrl::~CMaths2Ctrl()
  132. {
  133. }
  134.  
  135.  
  136. /////////////////////////////////////////////////////////////////////////////
  137. // CMaths2Ctrl::OnDraw - Drawing function
  138.  
  139. #define X(x) (int)(xLeft + (x)*xScale/100)    // Scaling macros
  140. #define Y(y) (int)(yTop + (y)*yScale/100)    // so scale is 0 - 100
  141. #define CX(x) (int)((x)*xScale/100)
  142. #define CY(y) (int)((y)*yScale/100)
  143.  
  144. void CMaths2Ctrl::OnDraw(
  145.             CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid)
  146. {
  147.     long xLeft = rcBounds.left;            // Use with scaling macros
  148.     long yTop = rcBounds.top;    
  149.     long xScale = rcBounds.Width();
  150.     long yScale = rcBounds.Height();    
  151.  
  152.     int iPenWidth = max(CX(5), CY(5)); // Pen width based on control size
  153.     CBrush brushBack;                   // Background brush
  154.     CPen penFore(PS_SOLID, iPenWidth, TranslateColor(GetForeColor()));
  155.     COLORREF crBack = TranslateColor(GetBackColor());    //Use BackColor
  156.     brushBack.CreateSolidBrush(crBack);                 //Stock Property
  157.  
  158.     pdc->FillRect(rcBounds, &brushBack);                // Clear background        
  159.  
  160.     CPen* pPenSave = pdc->SelectObject(&penFore);
  161.  
  162.     // Draw a / sign
  163.     pdc->MoveTo(xLeft + xScale - CX(10), yTop + CY(10));
  164.     pdc->LineTo(xLeft + CX(10), yTop + yScale - CY(10));        // Draw a line from top right to bottom left
  165.     
  166.     if (!m_bOpposite)
  167.     {
  168.         // Draw a X sign
  169.         pdc->MoveTo(xLeft + CX(10), yTop + CY(10));
  170.         pdc->LineTo(xLeft + xScale - CX(10), yTop + yScale - CY(10));        // Draw a line from top left to bottom right
  171.     }
  172.  
  173.     pdc->SelectObject(pPenSave);
  174. }
  175.  
  176.  
  177. /////////////////////////////////////////////////////////////////////////////
  178. // CMaths2Ctrl::DoPropExchange - Persistence support
  179.  
  180. void CMaths2Ctrl::DoPropExchange(CPropExchange* pPX)
  181. {
  182.     ExchangeVersion(pPX, MAKELONG(_wVerMinor, _wVerMajor));
  183.     COleControl::DoPropExchange(pPX);
  184.  
  185.     PX_Bool(pPX, CString((LPCTSTR)IDS_OPPOSITE), m_bOpposite, FALSE);
  186.  
  187. }
  188.  
  189.  
  190. /////////////////////////////////////////////////////////////////////////////
  191. // CMaths2Ctrl::OnResetState - Reset control to default state
  192.  
  193. void CMaths2Ctrl::OnResetState()
  194. {
  195.     COleControl::OnResetState();  // Resets defaults found in DoPropExchange
  196. }
  197.  
  198. /////////////////////////////////////////////////////////////////////////////
  199. // CMaths2Ctrl message handlers
  200.  
  201. void CMaths2Ctrl::OnLButtonDown(UINT nFlags, CPoint point) 
  202. {
  203.     CRect rcBounds;
  204.     GetClientRect(&rcBounds);
  205.  
  206.     long xLeft = rcBounds.left;            // Use with scaling macros
  207.     long yTop = rcBounds.top;    
  208.     long xScale = rcBounds.Width();
  209.     long yScale = rcBounds.Height();    
  210.     float gradient = (float)yScale / (float)xScale;
  211.  
  212.     // Is point on bottom-left to top-right line
  213.     if ((point.y >= (yTop + yScale - (gradient * point.x) - CY(5))) && (point.y <= (yTop + yScale - (gradient * point.x) + CY(5))))
  214.         FireInside((short)point.x, (short)point.y);
  215.     else
  216.     {
  217.         if (!m_bOpposite)
  218.         {
  219.             // Is point on top-left to bottom-right line
  220.             if ((point.y >= yTop + (gradient * point.x) - CY(5)) && (point.y <= yTop + (gradient * point.x) + CY(5)))
  221.                 FireInside((short)point.x, (short)point.y);
  222.             else
  223.                 FireOutside();
  224.         }
  225.         else
  226.             FireOutside();
  227.     }
  228.     
  229.     COleControl::OnLButtonDown(nFlags, point);
  230. }
  231.  
  232. //Simple Ellipse Hit-Testing
  233. BOOL CMaths2Ctrl::InEllipse(const CPoint& pt,
  234.     int x1, int y1, int x2, int y2)
  235. {
  236.     // Determine radii                       
  237.     double a = (x2 - x1) / 2;
  238.     double b = (y2 - y1) / 2;
  239.     
  240.     // Determine x, y
  241.     double x = pt.x - (x1 + x2) / 2;
  242.     double y = pt.y - (y1 + y2) / 2;
  243.     
  244.     // Apply ellipse formula
  245.     return ((x * x) / (a * a) + (y * y) / (b * b) <= 1);
  246. }
  247.  
  248. void CMaths2Ctrl::OnOppositeChanged() 
  249. {
  250.     InvalidateControl();
  251. }
  252. /////////////////////////////////////////////////////////////////////////////
  253. // CAboutDlg dialog
  254.  
  255. class CAboutDlg : public CDialog
  256. {
  257. // Construction
  258. public:
  259.     CAboutDlg(CWnd* pParent = NULL);   // standard constructor
  260.  
  261. // Dialog Data
  262.     //{{AFX_DATA(CAboutDlg)
  263.     enum { IDD = IDD_ABOUTBOX_Maths2 };
  264.         // NOTE: the ClassWizard will add data members here
  265.     //}}AFX_DATA
  266.  
  267.  
  268. // Overrides
  269.     // ClassWizard generated virtual function overrides
  270.     //{{AFX_VIRTUAL(CAboutDlg)
  271.     protected:
  272.     virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  273.     //}}AFX_VIRTUAL
  274.  
  275. // Implementation
  276. protected:
  277.  
  278.     // Generated message map functions
  279.     //{{AFX_MSG(CAboutDlg)
  280.     virtual BOOL OnInitDialog();
  281.     //}}AFX_MSG
  282.     DECLARE_MESSAGE_MAP()
  283. };
  284. /////////////////////////////////////////////////////////////////////////////
  285. // CAboutDlg dialog
  286.  
  287.  
  288. CAboutDlg::CAboutDlg(CWnd* pParent /*=NULL*/)
  289.     : CDialog(CAboutDlg::IDD, pParent)
  290. {
  291.     //{{AFX_DATA_INIT(CAboutDlg)
  292.         // NOTE: the ClassWizard will add member initialization here
  293.     //}}AFX_DATA_INIT
  294. }
  295.  
  296.  
  297. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  298. {
  299.     CDialog::DoDataExchange(pDX);
  300.     //{{AFX_DATA_MAP(CAboutDlg)
  301.         // NOTE: the ClassWizard will add DDX and DDV calls here
  302.     //}}AFX_DATA_MAP
  303. }
  304.  
  305.  
  306. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  307.     //{{AFX_MSG_MAP(CAboutDlg)
  308.     //}}AFX_MSG_MAP
  309. END_MESSAGE_MAP()
  310.  
  311. /////////////////////////////////////////////////////////////////////////////
  312. // CAboutDlg message handlers
  313.  
  314. BOOL CAboutDlg::OnInitDialog() 
  315. {
  316.     CDialog::OnInitDialog();
  317.     CenterWindow();
  318.     return TRUE;  // return TRUE unless you set the focus to a control
  319.                   // EXCEPTION: OCX Property Pages should return FALSE
  320. }
  321.  
  322.  
  323. /////////////////////////////////////////////////////////////////////////////
  324. // CMaths2Ctrl::AboutBox - Display an "About" box to the user
  325.  
  326. void CMaths2Ctrl::AboutBox()
  327. {
  328.     CAboutDlg about;
  329.     about.DoModal();
  330. }
  331.